package com.nartosoft.tubuloader;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JWindow;
import javax.swing.Timer;

/**
 * @author mf
 * 
 * The Dialog that is displayed, when all files have been uploaded (or failed).
 */
public class UploadDoneDialog extends JWindow implements ActionListener,
		EventListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JTextArea fileNameArea;

	private JPanel mainPanel;

	private int totalWidth;

	private int totalHeight;

	private int time;

	private int errors;

	private Vector<String> fileNames;

	private JLabel bottomLabel;

	public UploadDoneDialog(JFrame f, int time) {
		super(f);
		fileNames = new Vector<String>();
		this.errors = 0;
		this.time = time;
		this.buildDialog();
	}

	private void buildDialog() {
		mainPanel = new JPanel();
		mainPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		mainPanel.setLayout(new BorderLayout(3, 3));
		JLabel topLabel = new JLabel(" Upload complete");
		topLabel.setFont(new Font(null, Font.BOLD, 12));
		mainPanel.add(topLabel, BorderLayout.NORTH);
		bottomLabel = new JLabel(" Errors: 0");
		bottomLabel.setFont(new Font(null, Font.BOLD, 12));
		mainPanel.add(bottomLabel, BorderLayout.SOUTH);
		fileNameArea = new JTextArea("");
		mainPanel.add(fileNameArea, BorderLayout.CENTER);
		this.getContentPane().add(mainPanel);
		MouseListener closer = new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				UploadDoneDialog.this.setVisible(false);
				UploadDoneDialog.this.dispose();
			}
		};
		fileNameArea.addMouseListener(closer);
		this.addMouseListener(closer);
	}

	public void actionPerformed(ActionEvent e) {
		this.setVisible(false);
		this.dispose();
		System.exit(-1);
	}

	public void showDialog() {
		this.addFiles();
		bottomLabel.setText(" Errors: " + this.errors);
		this.totalWidth = mainPanel.getPreferredSize().width + 5;
		this.totalHeight = mainPanel.getPreferredSize().height;
		Point corner = getLowerRightScreenCorner();
		setBounds(
				corner.x - totalWidth - 10, 
				corner.y - totalHeight - 10, 
				totalWidth, 
				totalHeight + 3);
		this.setVisible(true);
		Timer timer = new Timer(time * 1000, this);
		timer.setRepeats(false);
		timer.start();
	}

	private Point getLowerRightScreenCorner() {
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		Insets insets = tk.getScreenInsets(GraphicsEnvironment
				.getLocalGraphicsEnvironment()
				.getDefaultScreenDevice()
				.getDefaultConfiguration());		
		return new Point(
				(int)d.getWidth() - insets.right, 
				(int)d.getHeight() - insets.bottom);
	}
	
	private void addFiles() {
		Iterator<String> it = this.fileNames.iterator();
		while (it.hasNext()) {
			fileNameArea.append(it.next());
			if (it.hasNext()) {
				fileNameArea.append("\n");
			}// if
		}
	}

	public void onError(String message) {
		this.errors++;
	}

	public void onUpload(String fileName, String url) {
		this.fileNames.add(fileName);
	}

	public void onAllFilesDone() {
		this.showDialog();
	}
}
